package tjs.study.notes.dotest.disruptor.parallel.spring;

import com.lmax.disruptor.RingBuffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DisruptorProducer {
    
    @Autowired
    private RingBuffer<DisruptorNotice> ringBuffer;
    
    public DisruptorProducer(RingBuffer<DisruptorNotice> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void sendJson(String noticeJson) {
        long sequence = ringBuffer.next();
        try {
            DisruptorNotice disruptorNotice = ringBuffer.get(sequence);
            disruptorNotice.setNoticeJson(noticeJson);
        } finally {
            ringBuffer.publish(sequence);
        }
    }

}